import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/login',
    component: () => import("@/components/Login.vue")
  }, {
    path: "/",
    redirect: "/login"
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }, {
    // 先展示Home组件，然后重定向了/welcome, 
    // 此时welcome组件就会在Home组件中找到路由占位符<router-view>来存放
    path: "/home",
    name: "Home",
    component: () => import("@/components/Home.vue"),
    redirect: "/welcome",
    children: [{
      path: "/welcome",
      component: () => import("@/components/Welcome.vue")
    }, {
      path: "/users",
      component: () => import("@/components/user/Users.vue")
    }, {
      path: "/roles",
      component: () => import("@/components/power/Roles.vue")
    }, {
      path: "/rights",
      component: () => import("@/components/power/Rights.vue")
    }, {
      path: "/categories",
      component: () => import("@/components/goods/Categories.vue")
    }, {
      path: "/params",
      component: () => import("@/components/goods/Params.vue")
    }, {
      path: "/goods",
      component: () => import("@/components/goods/List.vue"),
    }, {
      path: "/goods/add",
      component: () => import("@/components/goods/AddGoods.vue"),
    }, {
      path: "/orders",
      component: () => import("@/components/order/Order.vue")
    },{
      path: "/reports",
      component: () => import("@/components/report/Report.vue")
    }]
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (from.path == "/") return next();
  let token = window.sessionStorage.getItem("token");
  if (!token) return next("/login");
  next();
})

export default router